package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void delete(Long id) {
        // 删除员工
        employeeMapper.deleteByPrimaryKey(id);
        // 删除与员工有关系的角色  => 删除中间表中 数据
        employeeMapper.deleteRelation(id);
    }

    @Override
    @Transactional
    public void insert(Employee employee, Long[] roleIds) {
        employeeMapper.insert(employee);
        // 给员工插入对应的角色关系
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    @Transactional
    public void update(Employee employee, Long[] roleIds) {
        employeeMapper.updateByPrimaryKey(employee);
        // 当从编辑页面 点击保存按钮时,将之前的关系删除 不然之前的权限会叠加在一起
        employeeMapper.deleteRelation(employee.getId());
        // 插入角色关系
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }


    // 分页  通过分页插件 进行分页查询
    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        // 使用分页插件，传入当前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        // 返回查询一页展示的数据
        return new PageInfo<>(employeeMapper.selectForList(qo));
    }

    // 查询全部
    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    // 查询是否用户名重复
    @Override
    public boolean checkUsername(String username) {
        Employee employee = employeeMapper.checkUsername(username);
        return employee != null;
    }

    // 登录查询用户是否存在
    @Override
    public Employee login(String username, String password) {
        return employeeMapper.selectByUsernameAndPassword(username, password);
    }

    // 文件导出 =>文件下载
    @Override
    public Workbook exportXls() {
        // 创建 excel 文件
        Workbook wb = new HSSFWorkbook();
        // 创建 sheet
        Sheet sheet = wb.createSheet("员工名单");
        // 标题行
        Row row = sheet.createRow(0);
        // 设置内容到单元格中
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        // 查询员工数据
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            // 创建行(每个员工就是一行)
            row = sheet.createRow(i + 1);
            // 设置内容到单元格中
            row.createCell(0)
                    .setCellValue(employee.getName());
            row.createCell(1)
                    .setCellValue(employee.getEmail());
            row.createCell(2)
                    .setCellValue(employee.getAge());
        }
        return wb;
    }

    // 文件导入 =>文件上传
    @Override
    public void importXls(Workbook wb) {
        Sheet sheet = wb.getSheetAt(0);
        // 获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        // 从索引为 1 的行数开始读（忽略标题行）
        for (int i = 1; i <= lastRowNum; i++) {
            // 获取行
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            // 获取行数据，封装到员工对象的对应属性上
            employee.setUsername(row.getCell(0).getStringCellValue());
            employee.setName(row.getCell(1).getStringCellValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            employee.setAge(((int) row.getCell(3).getNumericCellValue()));
            employee.setPassword("1");
            // 调用保存方法
            employeeMapper.insert(employee);
        }
    }

    @Override
    public Employee queryByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

}
